import { createStore } from "vuex";

let defaultcity='北京'
// 在localStorage对象中初始化一个属性city的值
if (localStorage.city) {
	defaultcity=localStorage.city
}

export default createStore({
  state: {
	  cityname:defaultcity,type:1,cityid:0
  },
  mutations: {
	  changeCity(state,val){
		 state.cityname=val
		 localStorage.city=val
		 if (val=='北京') {
		 	state.cityid=0
		 } else if(val=='广州'){
		 	state.cityid=1
		 }else if(val=='上海'){
		 	state.cityid=2
		 }
	  },
	  gettype(state,val){
		 state.type=val
	  }
  },//必须由组件的事件触发才会调用
  getters:{},
  actions: {
	  //context是vuex内固定为action准备的与state同等效果的对象
	  ajaxNum(context){
		  //通过context调用mutations函数
		  context.commit('Num')
	  }
  },//异步操作：具体过程不在actions内，在mutations对象里
  modules: {},
});
